package cc.linker.steplink.crm.merchanth5.service.impl;

import cc.linker.steplink.crm.commons.constant.ContentConstant;
import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.model.SystemProperties;
import cc.linker.steplink.crm.commons.utils.NumberUtils;
import cc.linker.steplink.crm.merchanth5.dao.acs.JoinAgentDao;
import cc.linker.steplink.crm.merchanth5.service.acs.JoinAgentService;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.util.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 加盟代理
 * @Author: wuhangfeng
 * @Date: Created in 2019/9/23 19:48
 * @Version: 1.9.1
 */
@Service
public class JoinAgentServiceImpl implements JoinAgentService {

    @Autowired
    private JoinAgentDao joinAgentDao;

    @Autowired
    private HttpService httpService;

    @Autowired
    private SystemProperties systemProperties;

    @Override
    public Map<String, Object> getJoinAgentAccountInfo(String customerId, String orgId) throws Exception {
        Map<String, Object> params = new HashMap<>(3);
        params.put("customerId", customerId);
        params.put("orgId", orgId);
        //获取加盟代理余额信息
        Map<String, Object> data = new HashMap<>(5);
        if (joinAgentDao.getPromoterStatus(params) >= 1) {
            Map<String, Object> joinAgentInfo = joinAgentDao.getJoinAgentInfo(params);
            if (joinAgentInfo != null) {
                params.put("promoterTime", joinAgentInfo.get("promoterTime"));
                //获取邀请下级数量
                Integer subNum = joinAgentDao.getJoinAgentSonNums(params);
                data.put("subNum", subNum);
                //获取加盟代理累计收益信息
                Map<String, Object> joinAgentAccountInfo = joinAgentDao.getJoinAgentAccountInfo(params);
                data.put("customerId", customerId);
                data.put("balance", NumberUtils.fenToYuan(Long.valueOf(joinAgentInfo.get("balance").toString())));
                data.put("freeze", NumberUtils.fenToYuan(Long.valueOf(joinAgentInfo.get("freeze").toString())));
                data.put("sumIncome", NumberUtils.fenToYuan(Long.valueOf(joinAgentAccountInfo.get("sumIncome").toString())));
                data.put("tradeNum", joinAgentAccountInfo.get("tradeNum"));
            } else {
                throw GlobalException.build("您还不是加盟代理");
            }
        } else {
            throw GlobalException.build("您还不是加盟代理");
        }
        return data;
    }

    @Override
    public Map<String, Object> getJoinAgentTodayTradeInfo(String customerId, String orgId) throws Exception {
        Map<String, Object> params = new HashMap<>(2);
        params.put("customerId", customerId);
        params.put("orgId", orgId);
        params.put("today", 1);
        //获取加盟代理余额信息
        Map<String, Object> data = new HashMap<>(4);
        if (joinAgentDao.getPromoterStatus(params) >= 1) {
            Map<String, Object> joinAgentInfo = joinAgentDao.getJoinAgentInfo(params);
            if (joinAgentInfo != null) {
                params.put("promoterTime", joinAgentInfo.get("promoterTime"));
                //获取邀请下级数量
                Integer subNum = joinAgentDao.getJoinAgentSonNums(params);
                //获取加盟代理今天收益信息
                Map<String, Object> joinAgentAccountInfo = joinAgentDao.getJoinAgentAccountInfo(params);
                data.put("customerId", customerId);
                data.put("sumIncome", NumberUtils.fenToYuan(Long.valueOf(joinAgentAccountInfo.get("sumIncome").toString())));
                data.put("tradeNum", joinAgentAccountInfo.get("tradeNum"));
                data.put("subNum", subNum);
            } else {
                throw GlobalException.build("您还不是加盟代理");
            }
        } else {
            throw GlobalException.build("您还不是加盟代理");
        }
        return data;
    }

    @Override
    public Integer getMyShopGoodsCount(Map<String, Object> sqlparam) {
        return joinAgentDao.getMyShopGoodsCount(sqlparam);
    }

    @Override
    public List<Map<String, Object>> getMyShopGoods(Map<String, Object> sqlparam) {
        return joinAgentDao.getMyShopGoods(sqlparam);
    }

    @Override
    public Map<String, Object> myStoreList(Map<String, Object> params) {
        Map<String, Object> resultMap = new HashMap<>();
        params.put("article", 0);
        params.put("goodsType", "1");
        List<Map<String, Object>> goodsList = joinAgentDao.listMyGoods(params);
        resultMap.put("goods", goodsList);
        params.put("goodsType", "3,4");
        params.put("knowledge", 1);
        List<Map<String, Object>> knowledgeList = joinAgentDao.listMyGoods(params);
        params.remove("knowledge");
        resultMap.put("knowledge", knowledgeList);
        params.remove("goodsType");
        params.put("article", 1);
        List<Map<String, Object>> articleGoodsList = joinAgentDao.listMyGoods(params);
        StringBuilder sb = new StringBuilder();
        articleGoodsList.forEach(x -> {
            String articleId = (String) x.get("articleId");
            if (!StringUtils.isEmpty(articleId)) {
                sb.append(articleId);
                sb.append(",");
            }
        });
        Map<Object, Object> articleGoodsMap = articleGoodsList.stream()
                .collect(Collectors.toMap(x -> x.get("articleId"), y -> y.get("goodsId")));
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("ids", sb.toString());
        JSONObject jsonObject = (JSONObject) JSONArray.toJSON(reqMap);
        String url = systemProperties.getSync().getContentUrl() + ContentConstant.GET_ARTICLE_LIST_BY_IDS;
        String result;
        try {
            result = httpService.doPost(url, jsonObject.toJSONString(), ContentConstant.JSON_HEADER);
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
            throw GlobalException.build("内容库调用异常");
        }
        Map res = (Map) JSON.parse(result);
        if ("200".equals(res.get("code").toString())) {
            //noinspection unchecked
            List<Map<String, Object>> data = (List) res.get("data");
            data.forEach(x -> {
                String id = (String) x.get("id");
                Long goodsId = (Long) articleGoodsMap.get(id);
                x.put("goodsId", goodsId);
            });
            resultMap.put("article", data);
        }
        return resultMap;
    }
}
